package com.my.study.deallock;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author Carlos
 * @version 1.0
 * @Description 异步模式 - 饥饿
 *
 * 解决：不同任务类型，使用不同的线程池
 *
 * @date 2021/6/13 21:39
 **/
@Slf4j(topic = "c.TestHunger")
public class TestHunger {

    static List<String> MENU = Arrays.asList("A", "B", "C", "D");
    static Random RANDOM = new Random();
    static String cooking() {
        return MENU.get(RANDOM.nextInt(MENU.size()));
    }

    public static void main(String[] args) {
        ExecutorService order = Executors.newFixedThreadPool(1);
        ExecutorService dish = Executors.newFixedThreadPool(1);



        order.execute(() -> {
            log.debug("处理点餐...");
            Future<String> cook = dish.submit(() -> {
                log.debug("做菜");
                return cooking();
            });
            try {
                log.debug("上菜：{}", cook.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });


        order.execute(() -> {
            log.debug("处理点餐...");
            Future<String> cook = dish.submit(() -> {
                log.debug("做菜");
                return cooking();
            });
            try {
                log.debug("上菜：{}", cook.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });



    }
}
